/* 表单的校验脚本 */

/**
 * 提示信息
 */
var Project_Process = {
    P_1: '需求分析',
    P_2: '开发',
    P_3: '验收测试',
    P_4: '模拟测试',
    P_5: '上线',
    P_1_MESSAGE: '【需求分析】未填写',
    P_2_MESSAGE: '【开发】未填写',
    P_3_MESSAGE: '【验收测试】未填写',
    P_4_MESSAGE: '【模拟测试】未填写',
    P_5_MESSAGE: '【上线】未填写',
    P_0_MESSAGE: '已完成'
}

/**
 * 返回值
 */
var ResultMessage = {
    createSuccessResult: function() {
        var Result = {
            result: true,
            message: ""
        };
        return Result;
    },
    createErrorResult: function(message_) {
        if (!isNotNull(message_)) {
            message_ = "操作失败";
        }
        var Result = {
            result: false,
            message: message_
        }
        return Result;
    }
}

/**
 * 【条目拆分】批量修改
 * @param {*} selectIds ：选择的条目ID
 * @param {*} version ：修改的目标版本
 */
function batchUpdateClause(selectIds, version) {
    if (!isNotNull(selectId)) {
        return ResultMessage.createErrorResult("请选择要修改的条目");
    } else {
        var have = true;
        if (isNotNull(version)) {
            var array = splitText(selectIds, ";");
            var domainid = getDomainid();
            for (var n = 0; n < array.length; n++) {
                var sql = "select  '" + domainid + "'  as domainid, count(1) as item_count from tlk_project_ver_plan where ( " +
                    "concat(item_platform, '-', item_system, '-', item_sysnam_son, '-', item_plan_ver) ) = " +
                    "(select concat(ITEM_SXPT, '-', ITEM_XQSXXT, '-', ITEM_XQZXT, '-', '" + version + "') " +
                    "from tlk_clause_present where id = '" + array[n] + "')";
                var result = findBySQL(sql).getItemValueAsString("count");
                if (result == "0") {
                    have = false;
                    break;
                }
            }
        }
        if (!have) {
            return ResultMessage.createErrorResult("不允许跨系统修改版本");
        } else {
            return ResultMessage.createSuccessResult();
        }
    }
}


/**
 * 条目拆分节点调整评估人员校验
 * @param {*} notNullBoxes  不可以为空的评估人员文本框
 * @param {*} boxName 对应的评估人员名称
 */
function editAuditors(notNullBoxes, boxName) {
    var errorMsg = "";
    var result = true;
    if (null != notNullBoxes && null != boxName && notNullBoxes.length == boxName.length) {
        for (var i = 0; i < notNullBoxes.length; i++) {
            var str = getItemValueAsString(notNullBoxes[i]);
            if (!isNotNull(str)) {
                if (isNotNull(errorMsg)) {
                    errorMsg = errorMsg + "、" + boxName[i];
                } else {
                    errorMsg = boxName[i];
                }
                result = false;
            }
        }
        if (result) {
            return ResultMessage.createSuccessResult();
        } else {
            return ResultMessage.createErrorResult(errorMsg + "不能为空");
        }
    } else {
        return ResultMessage.createSuccessResult();
    }
}

//【预算回退】-是否可以创建子表单对象
function accessCreateSonForm(parentName, parentTableName) {
    var domainid = getDomainid();
    var pDoc = getParentDocument();
    var sql = "select '" + domainid + "' as domainid, count(1) as item_count from " + parentTableName + " where id = '" + pDoc.getId() + "'";
    var result = findBySQL(sql);
    var count = result.getItemValueAsString("count");
    if (!(null != count && "1".equals(count))) {
        return ResultMessage.createErrorResult("请先保存父表单-" + parentName);
    } else {
        return ResultMessage.createSuccessResult();
    }
}


/**
 * 【电子需求单】设计部门项目经理选择主配系统
 */
function selectSystem() {
    var value = getItemValueAsString("ztxt_mc");
    if (!isNotNull(value)) {
        return ResultMessage.createErrorResult("请选择主配系统");
    } else {
        return ResultMessage.createSuccessResult();
    }
}

/**
 * 【电子需求单-条目拆分】条目拆分提交校验
 * @param {*} doc 
 */
function clauseAccess(doc) {
    /* ----------- 1. 校验条目数量 ----------- */
    var childs = doc.getChilds("clause_present");
    if (null == childs || childs.size() <= 0) {
        return ResultMessage.createErrorResult("条目数量不能为0");
    }
    /* ----------- 2. 校验条目信息填写情况 ----------- */
    var sql =
        "select '" + domainid + "' AS domainid" +
        ", ( " +
        " select count(1) from tlk_clause_present where " +
        "(item_jhpgwcsj is null or item_jhpgwcsj = ''  " +
        " or item_xqlx is  null or item_xqlx = '' " +
        " or item_sxpt is  null or item_sxpt = '' " +
        " or item_xqsxxt is  null or item_xqsxxt = '' " +
        " or item_xqsxsl is  null or item_xqsxsl = '' " +
        " or item_jhsxbb is  null or item_jhsxbb = '' " +
        " or item_dqbb is  null or item_dqbb = '' " +
        " or item_jpcd is  null or item_jpcd = ''  " +
        " or item_qwsxsj is  null or item_qwsxsj = '' " +
        " or item_tcyy is  null or item_tcyy = '' " +
        " or item_xqbmfzr is  null or item_xqbmfzr = '' " +
        " )" +
        " and parent = '" + doc.getId() + "' " +
        " ) as item_count " +
        " from dual";
    var result = findBySQL(sql);
    var sjCount = result.getItemValueAsString("count");
    if (null != sjCount && sjCount != "0") {
        return ResultMessage.createErrorResult("条目评估未完成");
    }
    /* ----------- 3. 校验条目所选系统是否存在于主配系统中 ----------- */
    var zpxtsql = "select '" + getDomainid() + "' as domainid, concat('(\\'',replace(( " +
        " case when (item_phxt_mc is null or item_phxt_mc = '') " +
        " then item_ztxt_mc else concat(item_ztxt_mc, ';', item_phxt_mc) end), ';', '\\',\\''), '\\')') as item_zpxt_param" +
        " from tlk_setuo_carryout where id = '" + doc.getId() + "'";
    var zpxtResult = findBySQL(zpxtsql);
    var zpxtParam = zpxtResult.getItemValueAsString("zpxt_param");
    var zpxtsql2 = "select '" + getDomainid() + "' as domainid, count(1) as item_count from tlk_clause_present where parent = '" + doc.getId() + "' and " +
        " (case when (ITEM_XQZXT is null or ITEM_XQZXT = '') then  " +
        " item_xqsxxt else concat(item_xqsxxt, '_', ITEM_XQZXT) end) " +
        " not in " + zpxtParam;
    var zpxtResult2 = findBySQL(zpxtsql2);
    var zpxtCount = zpxtResult2.getItemValueAsString("count");
    if (null == zpxtCount || !"0".equals(zpxtCount)) {
        return ResultMessage.createErrorResult("存在条目所属系统不在所选主配系统中");
    }
    /* ----------- 4. 校验条目计划上下版本与所属系统是否匹配 ----------- */
    var versql = "select '" + getDomainid() + "' as domainid, count(1) as item_count from tlk_clause_present where parent = '" + doc.getId() + "' and " +
        " (case when (ITEM_XQZXT is null or ITEM_XQZXT = '') then  " +
        " concat(item_xqsxxt, item_jhsxbb) else concat(item_xqsxxt, '-', ITEM_XQZXT, item_jhsxbb) end) " +
        " not in (select item_proj_ver from tlk_project_ver_plan)";
    var verResult = findBySQL(versql);
    var verCount = verResult.getItemValueAsString("count");
    if (null == verCount || !"0".equals(verCount)) {
        return ResultMessage.createErrorResult("存在'" + verCount + "'个条目的计划上线版本与所选系统不匹配");
    }
    /* ----------- 5. 评估人员校验-非空校验 ----------- */
    var vBox1 = getItemValueAsString("xqtcbm_pgry");
    var vBox2 = getItemValueAsString("sjbm_pgry");
    var vBox3 = getItemValueAsString("kfgs_pgry");
    var vBox4 = getItemValueAsString("ywbm_pgry");
    if (!isNotNull(vBox1) && !isNotNull(vBox2) && !isNotNull(vBox3) && !isNotNull(vBox4)) {
        return ResultMessage.createErrorResult("评估人员不能全部为空");
    }
    /* ----------- 6. 评估人员校验-变更校验 ----------- */
    var vBox5 = getItemValueAsString("dzxqdfzr");
    var sql_pgry = "select '" + domainid + "' as domainid, item_dzxqdfzr, item_xqtcbm_pgry , item_sjbm_pgry , item_kfgs_pgry , item_ywbm_pgry  from tlk_setuo_carryout where id = '" + getCurrentDocument().getId() + "'";
    var result_pgry = findBySQL(sql_pgry);
    if (null != result_pgry) {
        var v1 = result_pgry.getItemValueAsString("xqtcbm_pgry");
        var v2 = result_pgry.getItemValueAsString("sjbm_pgry");
        var v3 = result_pgry.getItemValueAsString("kfgs_pgry");
        var v4 = result_pgry.getItemValueAsString("ywbm_pgry");
        var v5 = result_pgry.getItemValueAsString("dzxqdfzr");
        if (!v1.equals(vBox1) || !v2.equals(vBox2) || !v3.equals(vBox3) || !v4.equals(vBox4) || !v5.equals(vBox5)) {
            return ResultMessage.createErrorResult("评估人员已经修改，请保存后再提交");
        }
    }
    /* ----------- 7. 校验构件采购清单 ----------- */
    var sqdhhh = getCurrentDocument().getItemValueAsString("sqdh");
    var gjsql1 = "select count(*) as item_count, '" + getDomainid() + "' as domainid  " +
        " from tlk_setuo_carryout s,tlk_component_purchase_list c where s.id=c.PARENT and " +
        "s.ITEM_sqdh='" + sqdhhh + "'";
    var gjfind = findBySQL(gjsql1);
    if (null != gjfind) {
        var gjcount = gjfind.getItemValueAsString("count");
        if (!"0".equals(gjcount)) {
            var gjsql2 = "select count(1) as item_count, '" + getDomainid() +
                "' as domainid  from tlk_setuo_carryout  s,tlk_component_purchase_list c where s.id=c.PARENT and  s.ITEM_sqdh = '" + sqdhhh + "' and ( (c.ITEM_GJMC is null or c.ITEM_GJMC = '') or (c.ITEM_SYXT is null or c.ITEM_SYXT = '')  or (c.ITEM_BB is null or c.ITEM_BB = ''))";
            var gjfind2 = findBySQL(gjsql2);
            if (null != gjfind2) {
                var gjcount2 = gjfind2.getItemValueAsString("count");
                if (!"0".equals(gjcount2)) {
                    return ResultMessage.createErrorResult("构件采购清单基本信息不齐全");
                }
            }
        }
    }
    return ResultMessage.createSuccessResult();
}

/**
 * 【电子需求单-工作量评估】工作量评估提交校验
 *
 * @param doc
 *
 * @Description:
 * 1. 工作量评估是否完成
 * 2. 预算工作量是否超出总量
 * 3. 期数是否填写完整
 * 4. 进度反馈情况
 */
function workloadAccess(doc) {
    var domainid = getDomainid();
    //工作量评估是否完成
    var childs1 = doc.getChilds("workload_assess_info");
    if (null != childs1 && childs1.size() > 0) {
        for (var t = 0; t < childs1.size(); t++) {
            var child = childs1.get(t);
            var state = child.getItemValueAsString("fkzt");
            if (!(null != state && "已完成".equals(state))) {
                return ResultMessage.createErrorResult("工作评估未完成");
            }
        }
    }
    childs1 = doc.getChilds("workload_assess_info");
    //监测进度填写情况
    for (var i = 0; i < childs1.size(); i++) {
        var child = childs1.get(i);
        var xtbb = child.getItemValueAsString("xtbb");
        var sql_2 = "select '" + domainid + "' as domainid, id as item_id from tlk_project_ver_plan where " +
            " ( " +
            " case when (item_sysnam_son is null or item_sysnam_son = '') " +
            " then concat(item_system, item_plan_ver) " +
            " else concat(item_system, '-', item_sysnam_son, item_plan_ver) end " +
            " )   = '" + xtbb + "' ";
        var result_2 = findBySQL(sql_2);
        if (isNotNull(result_2)) {
            var value = "";
            var pid = result_2.getItemValueAsString("id");
            var sql_1 = "" +
                " select '" + domainid + "' as domainid, ( " +
                " case when ( " +
                " (select count(1) from tlk_project_plan where parent = '" + pid + "' " +
                " and item_stage = '" + Project_Process.P_1 + "' and (item_plan_start is null or item_plan_start = '')) = 1 " +
                " ) " +
                " then '" + Project_Process.P_1_MESSAGE + "' "

            +" when ( " +
            " (select count(1) from tlk_project_plan where parent = '" + pid + "'  " +
                " and item_stage = '" + Project_Process.P_2 + "' and (item_plan_start is null or item_plan_start = '')) = 1 " +
                " ) " +
                " then '" + Project_Process.P_2_MESSAGE + "' "

            +
            " when ( " +
            " (select count(1) from tlk_project_plan where parent = '" + pid + "'  " +
                " and item_stage = '" + Project_Process.P_3 + "' and (item_plan_start is null or item_plan_start = '')) = 1 " +
                " ) " +
                " then '" + Project_Process.P_3_MESSAGE + "' "

            +
            " when ( " +
            " (select count(1) from tlk_project_plan where parent = '" + pid + "'  " +
                " and item_stage = '" + Project_Process.P_4 + "' and (item_plan_start is null or item_plan_start = '')) = 1 " +
                " ) " +
                " then '" + Project_Process.P_4_MESSAGE + "' "

            +
            " when ( " +
            " (select count(1) from tlk_project_plan where parent = '" + pid + "'  " +
                " and item_stage = '" + Project_Process.P_5 + "' and (item_plan_start is null or item_plan_start = '')) = 1 " +
                " ) " +
                " then '" + Project_Process.P_5_MESSAGE + "' "

            +
            " else '" + Project_Process.P_0_MESSAGE + "' end ) as item_result " +
                " from dual ";
            var result_1 = findBySQL(sql_1);
            if (null != result_1) {
                value = result_1.getItemValueAsString("result");
            }
            if (!(null != value && value.equals(Project_Process.P_0_MESSAGE))) {
                return ResultMessage.createErrorResult("进度填写未完成");
            }

        }
    }

    //期数是否填写完整
    var childs2 = doc.getChilds("project_scheme");
    var projects = "";
    if (null != childs2 && childs2.size() > 0) {
        for (var n = 0; n < childs2.size(); n++) {
            var child = childs2.get(n);
            var qs = child.getItemValueAsString("qishu");
            if (!isNotNull(qs)) {
                var system = child.getItemValueAsString("shixianxitong");
                var son = child.getItemValueAsString("shixianzixitong");
                var version = child.getItemValueAsString("shangxianbanben");
                var msg = (isNotNull(son)) ? (system + "-" + son + version) : (system + version);
                projects = (isNotNull(projects)) ? (projects + "、" + msg) : (msg);
            }
        }
    }
    if (isNotNull(projects)) {
        return ResultMessage.createErrorResult("项目整体安排计划需要填写期数");
    }
    //预算工作量是否超出总量
    var ywlx = doc.getItemValueAsString("ywlx");
    if ("常规完善类".equals(ywlx)) {
        var v1 = doc.getItemValueAsDouble("bclxgzl");
        var v2 = doc.getItemValueAsDouble("kyl");
        if (v1 > v2) {
            return ResultMessage.createErrorResult("工作量超出可用值");
        }
    }

    //折算人月是否为空
    var zsry_sql = "select count(1) as item_count, '" + getDomainid() + "' as domainid from tlk_Component_Purchase_List cpl where cpl.parent = '" + getCurrentDocument().getId() + "' and (item_zsry = '' or item_zsry is null)";
    var zsry_result = findBySQL(zsry_sql);
    var zsry_count = (zsry_result != null) ? zsry_result.getItemValueAsString("count") : "";
    if (!"0".equals(zsry_count)) {
        return ResultMessage.createErrorResult("折算人月不能为空");
    }
    return ResultMessage.createSuccessResult();
}